package utils

import (
	"context"
	"fmt"
	"github.com/olivere/elastic/v7"
	"github.com/spf13/viper"
)

// Client https://blog.csdn.net/leo_jk/article/details/123915677

var ElasticClient *elastic.Client

var host = "http://172.29.165.181:9200/"
var ElasticResp *elastic.Response
var DoCtxBackground = context.Background()

// InitESClient 初始化ES
func InitESClient() {
	host = viper.GetString("es_connect.host")
	var err error
	//初始化客户端
	ElasticClient, err = elastic.NewClient(
		elastic.SetSniff(false),
		elastic.SetURL(host),
	)
	if err != nil {
		fmt.Println("报错信心", err)
		panic(err)
	}
	//连接服务测试
	_, _, err = ElasticClient.Ping(host).Do(context.Background())
	if err != nil {
		fmt.Println("连接服务测试", err)
		panic(err)
	}
	//fmt.Printf("Elasticsearch returned with code %d and version %s\n", code, info.Version.Number)
	//获取服务端版本号
	esversion, err := ElasticClient.ElasticsearchVersion(host)
	if err != nil {
		fmt.Println("获取服务端版本号", err)
		panic(err)
	}
	fmt.Printf("Elasticsearch version %s\n", esversion)
}
